package com.meituan.catering.management.shop.biz.service.impl;

import com.meituan.catering.management.shop.biz.service.ShopBusinessNoBizService;
import com.meituan.catering.management.shop.dao.mapper.ShopMapper;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

/**
 * 关于门店业务号的实现
 */
@Service
public class ShopBusinessNoServiceImpl implements ShopBusinessNoBizService {

    private static final int MAX_RETRY_COUNT = 10;
    @Resource
    private ShopMapper shopMapper;

    public ShopBusinessNoServiceImpl() {
    }

    public String generate() {
        int var1 = 0;

        String businessNo;
        boolean hasConflict;
        do {
            if (var1++ >= 10) {
                throw new IllegalStateException("经过10次随机选取也无法获取不重复的商户号");
            }

            businessNo = RandomStringUtils.randomNumeric(MAX_RETRY_COUNT);
            hasConflict = this.shopMapper.countByBusinessNo(businessNo) > 0;
        } while(hasConflict);

        return businessNo;
    }

}
